package org.xx.armory.swing.controllers;

import org.xx.armory.swing.builders.ButtonModel;
import org.xx.armory.swing.builders.UIModel;
import org.xx.armory.swing.components.AbstractPane;

import javax.swing.*;

public abstract class PaneUIModelController<T extends AbstractPane>
        extends UIModelController<T> {
    public PaneUIModelController(
            T container
    ) {
        super(container);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void loadFromUIModel(
            UIModel uiModel
    ) {
        super.loadFromUIModel(uiModel);

        createTitle(uiModel);
        createIcon(uiModel);
        createBorder(uiModel);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected JButton createButton(
            ButtonModel model
    ) {
        JButton result = super.createButton(model);

        if (model.isDefault()) {
            this.getContainer().setDefaultButton(result);
        }

        return result;
    }

    protected void createBorder(
            UIModel uiModel
    ) {
        getContainer().setBorder(BorderFactory.createEmptyBorder(this.margin, this.margin, this.margin, this.margin));
    }

    protected void createTitle(
            UIModel uiModel
    ) {
        getContainer().setTitle(uiModel.getTitle());
    }

    protected void createIcon(
            UIModel uiModel
    ) {
        getContainer().setIcon(loadImage(uiModel.getResourceName(), uiModel.getIcon()));
    }
}
